let ejs = require("ejs");
let mysql = require("mysql");
let server = require("express")();
const expressStatic = require("express-static");

// 规划链接
let sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'chuanyue'
});

// 尝试链接
sql.connect();

//跳转case页面
server.get("/case", function(request, response) {
    // let id = request.query.id;
    sql.query(`SELECT * FROM caselist`, function(error, data) {
        if (error) {
            console.log(error);
            return;

        }

        ejs.renderFile(`static/case.html`, { caseList: data }, function(error, result) {
            if (error) {
                console.log(error);
                return;

            }
            // console.log(result);
            console.log("case页面加载成功");

            response.end(result);

        })
    })
})

//跳转project页面
server.get("/project", function(request, response) {

    sql.query(`SELECT * FROM projectlist`, function(error, data) {
        if (error) {
            console.log(error);
            return;

        }

        ejs.renderFile(`static/project.html`, { projectList: data }, function(error, result) {
            if (error) {
                console.log(error);
                return;

            }
            // console.log(result);
            console.log("project页面加载成功");

            response.end(result);

        })
    })
})


//跳转case detail页面
server.get("/casedetail", function(request, response) {
    let id = request.query.id;
    sql.query(`SELECT * FROM casedetail WHERE id=${id}`, function(error, data) {
        if (error) {
            console.log(error);
            return;

        }

        ejs.renderFile(`static/case_detail.html`, { caseDetail: data }, function(error, result) {
            if (error) {
                console.log(error);
                return;

            }
            // console.log(result);
            console.log("case detail页面加载成功");

            // console.log(result);

            response.end(result);

        })
    })
})

//跳转project detail页面
server.get("/projectdetail", function(request, response) {
    let id = request.query.id;
    sql.query(`SELECT * FROM projectdetail WHERE id=${id}`, function(error, data) {
        if (error) {
            console.log(error);
            return;

        }

        ejs.renderFile(`static/project_detail.html`, { projectDetail: data }, function(error, result) {
            if (error) {
                console.log(error);
                return;

            }
            // console.log(result);
            console.log("project detail页面加载成功");

            // console.log(result);

            response.end(result);

        })
    })
})

// 拦截所有请求，并且声明静态请求目录
server.use(expressStatic(`${__dirname}/static`));

server.listen(8080)